package com.ruoyi.ebike.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ebike.domain.Vehicle;
import com.ruoyi.ebike.service.IVehicleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车辆列表Controller
 *
 * @author ruoyi
 * @date 2024-12-18
 */
@RestController
@RequestMapping("/ebike/vehicle")
public class VehicleController extends BaseController
{
    @Autowired
    private IVehicleService vehicleService;

    /**
     * 查询车辆列表列表
     */
    @PreAuthorize("@ss.hasPermi('ebike:vehicle:list')")
    @GetMapping("/list")
    public TableDataInfo list(Vehicle vehicle)
    {
        startPage();
        List<Vehicle> list = vehicleService.selectVehicleList(vehicle);
        return getDataTable(list);
    }

    /**
     * 导出车辆列表列表
     */
    @PreAuthorize("@ss.hasPermi('ebike:vehicle:export')")
    @Log(title = "车辆列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Vehicle vehicle)
    {
        List<Vehicle> list = vehicleService.selectVehicleList(vehicle);
        ExcelUtil<Vehicle> util = new ExcelUtil<Vehicle>(Vehicle.class);
        util.exportExcel(response, list, "车辆列表数据");
    }

    /**
     * 获取车辆列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('ebike:vehicle:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        Vehicle vehicle = vehicleService.selectVehicleById(id);
        return success(vehicle);
    }

    /**
     * 新增车辆列表
     */
//    @PreAuthorize("@ss.hasPermi('ebike:vehicle:add')")
//    @Log(title = "车辆列表", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody Vehicle vehicle)
//    {
//        return toAjax(vehicleService.insertVehicle(vehicle));
//    }

    /**
     * 修改车辆列表
     */
    @PreAuthorize("@ss.hasPermi('ebike:vehicle:edit')")
    @Log(title = "车辆列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Vehicle vehicle)
    {
        return toAjax(vehicleService.updateVehicle(vehicle));
    }

    /**
     * 删除车辆列表
     */
    @PreAuthorize("@ss.hasPermi('ebike:vehicle:remove')")
    @Log(title = "车辆列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(vehicleService.deleteVehicleByIds(ids));
    }


    /**
     * 车辆运营看板
     */
    @PreAuthorize("@ss.hasPermi('ebike:vehicle:query')")
    @GetMapping("/statusCount")
    public Map<String, Long> getStatusCount() {
        return vehicleService.getVehicleStatusCount();
    }

    /**
     * 统计各区域车辆投放数量
     */
    @PreAuthorize("@ss.hasPermi('ebike:vehicle:query')")
    @GetMapping("/areaCount/{deptId}")
    public ResponseEntity<BigDecimal> getAreaCountByDeptId(@PathVariable Long deptId) {
        BigDecimal count = vehicleService.getAreaCountByDeptId(deptId);
        return ResponseEntity.ok(count);
    }

    /**
     * 统计各区域运营收入
     */
    @PreAuthorize("@ss.hasPermi('ebike:vehicle:query')")
    @GetMapping("/areaIncome/{deptId}")
    public ResponseEntity<BigDecimal> getAreaIncomeByDeptId(@PathVariable Long deptId) {
        BigDecimal income = vehicleService.getAreaIncomeByDeptId(deptId);
        return ResponseEntity.ok(income);
    }



}
